import axios from 'axios'
import utils from '../utils'

// post 常用 contentType 定义
const headers = {
  formData:{
    headers: {'Content-Type': 'multipart/form-data'}
  },
  json: {
    headers: {'Content-Type': 'application/json'}
  },
  urlencoded:{
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  }
};

axios.defaults.headers['Cache-Control'] = 'no-cache';
axios.defaults.headers['Pragma'] = 'no-cache';
// axios.defaults.baseURL = 'http://localhost:8081/sanritools'

axios.interceptors.request.use(config => {
  console.log(utils.getRootPath(),'-----------')
  config.url = '/sanritools'+config.url;

  return config;
},error => {
  return Promise.reject(error)
});

export default {
  // download: '/file/manager/downloadPath',
  download(modul,baseName,extend,callback){
    axios.get('/file/manager/downloadPath',{ responseType: 'blob',params:{baseName:baseName,modul: modul}}).then(res => {
      let blob = new Blob([res.data], {type: 'application/octet-stream'});
      let fileName = baseName;
      if(fileName.indexOf('/') !== -1){
        fileName = fileName.substr(fileName.lastIndexOf('/')+1);
      }
      fileName += ('.'+extend);

      if (navigator.appVersion.toString().indexOf('.NET') > 0) {
        window.navigator.msSaveBlob(blob, fileName)
      } else {
        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;
        link.style.display = 'none';
        document.body.appendChild(link);
        link.click();

        URL.revokeObjectURL(link.href);
        document.body.removeChild(link);
      }

      if(callback && typeof callback == 'function'){
        callback();
      }
    });
  },
  index:{
    listTools() {return axios.get('/index/listTools');},
    toolscount() {return axios.get('/index/toolCount');},
    reloadConfig(){return axios.get('/index/reloadConfig');},
  },
  nametool:{
    modul:'translate',
    tags(){return axios.get('/file/manager/simpleConfigNames',{params:{modul:this.modul}});},
    tagContent(tag){return axios.get('/file/manager/readConfig',{params:{modul:this.modul,baseName:tag}});},
    translate(text,biz,englishs){return axios.get('/translate/translate',{params:{biz: biz, orginChars: text, englishs: englishs}});},
    saveTag(bizData){
      return axios.post('/file/manager/writeConfig',Object.assign({modul:this.modul,baseName:bizData.tag},bizData),headers.json);
    }
  },
  tablehelp:{
    connects(){return axios.get('/sqlclient/connections');},
    schemas(connect){return axios.get('/sqlclient/schemas',{params:{connName:connect}});},
    tables(connect,schema){return axios.get('/sqlclient/tables',{params:{connName:connect,schemaName:schema}});},
    columns(connect,schema,table){return axios.get('/sqlclient/refreshTable',{params:{connName:connect,schemaName:schema,tableName:table}})},
    searchTable(connect,schema,keyword){return axios.get('/sqlclient/searchTables',{params:{connName:connect,schemaName:schema,keyword:keyword}})},
    code:{
      tkmybatis(connect,schema,configs){
        let data = {
          baseMapper: configs.baseMapper,
          projectName: configs.projectName,
          connectionConfig: {
            connName: connect,
            schemaName: schema,
            tableNames: configs.tableNames
          },
          packageConfig: {
            base: configs.base,
            mapper: configs.mapper,
            entity: configs.entity,
            service: configs.service,
            controller: configs.controller,
            vo: configs.vo,
            dto: configs.dto,
            param: configs.param
          },
          entityConfig: {
            baseEntity: configs.extends,
            interfaces: configs.implements,
            excludeColumns: configs.excludes.split(','),
            supports: configs.supports,
            idColumn: configs.idColumn,
            sqlStatement: configs.sqlStatement,
            tkMapper:configs.tkMapper
          },
          mavenConfig: {
            groupId: configs.groupId,
            artifactId: configs.artifactId,
            version: configs.version,
            springBootVersion: configs.springBootVersion
          }
        };
        // 发送数据请求
        return axios.post('/mybatis/code/projectBuild',{codeGeneratorConfig:data},headers.json);
      }
    }
  }
};
